/* eslint-disable prettier/prettier */
// import RNFS from 'react-native-fs';
import { BUshou } from "../enums/constant";
export interface button  {
    title: string;
    value: number;
}
export const getbuttons = async (bushou): Promise<button[]> => {
  const i = bushou.length;
  const button1 = bushou.map(bushou => ({
    title: bushou,
    value: 0,
  }));
  const allRadicals = BUshou.ALL_RADICALS;
  // 打乱 allRadicals 数组
  const shuffledAllRadicals = shuffle1([...allRadicals]);
  console.log(shuffledAllRadicals);
  // 过滤掉 bushou 数组中已有的偏旁
  const remainingRadicals = shuffledAllRadicals.filter(radical => !bushou.includes(radical));
  // 确保剩余偏旁的数量足以生成按钮列表
  const neededCount = 6 - i; // 假设你需要10个按钮
  const additionalRadicals = remainingRadicals.slice(0, neededCount);
  // 生成按钮数组
  console.log(additionalRadicals);
  const buttons = additionalRadicals.map((radical, index) => ({
    title: radical,
    value: index,
  }));
  const combinedButtons = button1.concat(buttons);
  console.log(combinedButtons + ";;;;");
  
  return shuffle(combinedButtons);
};
function shuffle1(array:string[]) {
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
  return array;
}

function shuffle(array:button[]) {
    let currentIndex = array.length;
    while (currentIndex > 0) {
      const randomIndex = Math.floor(Math.random() * currentIndex);
      currentIndex -= 1;
      const temporaryValue = array[currentIndex];
      array[currentIndex] = array[randomIndex];
      array[randomIndex] = temporaryValue;
    }
    return array;
  }
// 打乱 buttons 数组的顺序
// Fisher-Yates 洗牌算法 
 
// 打乱按钮数组
